package mt.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import mt.Order;
import mt.comm.ClientComm;
/**
 *Provides the necessary tools for the user to buy/sell his/her orders.
 * 
 * @author Grupo 2
 */
public class MicroTraderOrder {

	private JPanel nnorthpanel;
	private JFrame frame= new JFrame();
	private boolean isBuyOrder;
	private JTextField stock = new JTextField("Stock that you want");
	private SpinnerNumberModel limits = new SpinnerNumberModel(1, 1, 9999, 1);  
	private JSpinner number = new JSpinner(limits);
	private JTextField price = new JTextField("Price per stock");
	private JButton orderButton;
	private ClientComm clientComm;
	private DefaultListModel<String> historyModel;
	private String order;

	/**
	 * Contructor for the GUI.
	 * 
	 * @param clientComm	Object through which the client can communicate the server.
	 * @param user			Name of the user.
	 * @param isBuyOrder	If true the GUI should be able to provide means to buy orders. If false it should provide the means to sell orders.
	 * @param order			Information/name of the order.
	 * @param historyModel	Object of the history list .
	 */
	public MicroTraderOrder(ClientComm clientComm, final String user, boolean isBuyOrder, String order, DefaultListModel<String> historyModel, DefaultListModel<String> marketModel){
		this.order=order;
		this.historyModel=historyModel;
		this.clientComm = clientComm;
		orderButton = new JButton("Place order");
		this.isBuyOrder = isBuyOrder;
		frame.setSize(new Dimension(300,400));
		frame.setLayout(new BorderLayout());
		frame.setLocationRelativeTo(null);
		nnorthpanel = new JPanel();
		nnorthpanel.setLayout(new GridLayout(4,1));


		nnorthpanel.add(new JLabel("Place your order: "));
		stock.setText(order);
		nnorthpanel.add(stock);
		nnorthpanel.add(number);
		nnorthpanel.add(price);
		price.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				price.setText("");
			}
		});
		if(isBuyOrder){
			orderButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					try{
						if(Double.parseDouble(price.getText())>0 && !stock.getText().isEmpty()){
							getClientComm().sendOrder(Order.createBuyOrder(user,
									stock.getText(),
									(Integer)number.getValue()
									, Double.parseDouble(price.getText())));
							System.out.println("Order sent");
							updateHistoryList("BOUGHT "+ stock.getText() +" "+(Integer)number.getValue() + " stocks " +Double.parseDouble(price.getText()) +" each");
							frame.dispose();
						}else{
							JOptionPane.showOptionDialog(frame,"Stock not chosen","",JOptionPane.PLAIN_MESSAGE,JOptionPane.WARNING_MESSAGE,null, null, null);
						}
					}catch(NumberFormatException e){
						JOptionPane.showOptionDialog(frame,"Price per stock is empty or invalid","",JOptionPane.PLAIN_MESSAGE,JOptionPane.WARNING_MESSAGE,null, null, null);
					}
				}});

		}else{
			orderButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					try{
						if(Double.parseDouble(price.getText())>0 && !stock.getText().isEmpty()){
							getClientComm().sendOrder(Order.createSellOrder(user,
									stock.getText(),
									(Integer)number.getValue()
									, Double.parseDouble(price.getText())));
							System.out.println("Order sent");
							updateHistoryList("SOLD "+ stock.getText() +" "+(Integer)number.getValue() + " stocks " +Double.parseDouble(price.getText()) +" each");
							frame.dispose();
						}else{
							JOptionPane.showOptionDialog(frame,"Stock not chosen","",JOptionPane.PLAIN_MESSAGE,JOptionPane.WARNING_MESSAGE,null, null, null);
						}
					}catch(NumberFormatException e){
						JOptionPane.showOptionDialog(frame,"Price per stock empty or invalid","",JOptionPane.PLAIN_MESSAGE,JOptionPane.WARNING_MESSAGE,null, null, null);
					}
				}});
		}



		nnorthpanel.setVisible(true);

		frame.add(nnorthpanel, BorderLayout.NORTH);
		frame.add(orderButton, BorderLayout.SOUTH);
		frame.setResizable(false);
		frame.setVisible(true);

	}
	
	/**
	 * Updates history list.
	 * @param str	information about the order to be added.
	 */
	private void updateHistoryList(String str) {
		System.out.println(str);
		historyModel.addElement(str);
	}
	/**
	 * Getter of the object
	 * @return clientComm	Object which allows communication with the server.	
	 */
	private ClientComm getClientComm(){ return clientComm;}


	/**
	 * Getter of the object
	 * @return order		Object which holds the information of the order to submit.
	 */
	public String getOrder() {
		return order;
	}
}




